const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    default_img: {},
    showType: 2,
    category_id: 0,
    goods_list: {}, //商品列表
    goodsCategoryList: [], //分类列表
    category_brands: [], //品牌列表
    search_text: '', //搜索内容
    categoryStatus: 0,
    attr_or_spec: [], //属性筛选项
    goods_spec_array: [], //规格筛选项
    sortStatus: 0,
    screenStatus: 0,
    maskStatus: 0,
    category_index: -1,
    salesSort: 'desc',
    newSort: 'asc',
    priceSort: 'asc',
    obyzd: '',
    brand_id: '',
    min_price: '',
    max_price: '',
    new_min_price: '',
    new_max_price: '',
    screen: 0,
    next_page: 2, //下一页,
    listClickFlag: 0,
    searchFlag: 0,
    attr: '',
    spec: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    let siteBaseUrl = getApp().globalData.siteBaseUrl;
    let url = '';
    let category_id = '';
    let brand_id = '';
    if (options.id != undefined) {
      category_id = options.id;
    }

    if (options.brand_id != undefined) {
      brand_id = options.brand_id;
      console.log(brand_id);
      that.setData({
        brand_id: brand_id,
      })
    }
    if (options.search_text != undefined) {
      let search_text = options.search_text;
      that.setData({
        search_text: search_text,
      })
    }
    let category_alias = options.alias;

    if (category_alias != '' && category_alias != undefined) {
      wx.setNavigationBarTitle({
        title: category_alias,
      })
    }

    that.setData({
      category_id: category_id,
    })

    app.defaultImg(that, true);

    app.sendRequest({
      url: 'System.Goods.goodsCategoryTree',
      data: {
        category_id: category_id,
      },
      success: function(res) {
        if (res.code == 0) {
          that.setData({
            goodsCategoryList: res.data
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this;
    app.restStatus(that, 'listClickFlag');
    app.restStatus(that, 'searchFlag');
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let that = this;
    let page = that.data.next_page;
    let category_id = that.data.category_id;
    let obyzd = that.data.obyzd;
    let salesSort = that.data.salesSort;
    let newSort = that.data.newSort;
    let priceSort = that.data.priceSort;
    let st = '';
    let brand_id = that.data.brand_id;
    let attr = that.data.attr;
    let spec = that.data.spec;
    let min_price = that.data.min_price;
    let max_price = that.data.max_price;
    let keyword = that.data.search_text;

    if (obyzd == 'sales') {
      st = salesSort;

    } else if (obyzd == 'is_new') {
      st = newSort;

    } else if (obyzd == 'price') {
      st = priceSort;

    }

    app.sendRequest({
      url: 'System.Goods.goodsListByConditions',
      data: {
        page_index: page,
        category_id: category_id,
        brand_id: brand_id,
        order: obyzd,
        sort: st,
        attr:attr,
        spec:spec,
        min_price: min_price,
        max_price: max_price,
        keyword : keyword
        
      },
      success: function(res) {
        let code = res.code;
        let data = res.data;
        if (code == 0) {
          let new_goods_list = data.goods_list.data;

          if (new_goods_list[0] != undefined) {
            page++;

            let goods_list = that.data.goods_list;
            let index = goods_list.length;

            let k = {};
            let parm = '';
            for (let key in new_goods_list) {
              let img = new_goods_list[key].pic_cover_small;
              new_goods_list[key].pic_cover_small = app.IMG(img);
              parm = 'goods_list[' + index + ']';
              k[parm] = new_goods_list[key];
              index++;
            }
            parm = 'next_page';
            k[parm] = page;

            that.setData(k)
          }
        }
      }
    });
  },

  /**
   * 图片加载失败
   */
  errorImg: function(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let goods_list = that.data.goods_list;
    let default_img = that.data.default_img;
    let base = that.data.Base;
    let parm = {};
    let img = goods_list[index].pic_cover_small;

    if (default_img.is_use == 1) {
      let default_goods_img = default_img.value.default_goods_img;
      if (img.indexOf(default_goods_img) == -1) {
        let parm_key = "goods_list[" + index + "].pic_cover_small";

        parm[parm_key] = default_goods_img;
        that.setData(parm);
      }
    }
  },


  /**
   * 输入框绑定事件
   */
  searchInput: function(event) {
    let search_text = event.detail.value;
    this.setData({
      search_text: search_text
    })
  },

  /**
   * 搜索商品
   */
  search: function(event) {
    let that = this;
    let searchFlag = that.data.searchFlag;
    let keyword = that.data.search_text;

    if (searchFlag == 1) {
      return false;
    }
    app.clicked(that, 'searchFlag');
    app.sendRequest({
      url: 'System.Goods.goodsListByConditions',
      data: {
        page_index: 1,
        category_id: '',
        brand_id: '',
        order: '',
        sort: '',
        attr: '',
        spec: '',
        min_price: null,
        max_price: null,
        keyword: keyword

      },
      success:function(res){
        let code = res.code;
        let data = res.data;
        if (code == 0) {
          let goods_list = data.goods_list.data;
          if(app.checkArray(goods_list)){
            for (let index in goods_list) {
              let img = goods_list[index].pic_cover_small;
              goods_list[index].pic_cover_small = app.IMG(img);
            }
          }

          that.setData({
            goods_list : goods_list,
            search_text : keyword
          })
        }
      }
    })
    
  },

  /**
   * 展示类型(1块展示 2列表展示)
   */
  showType: function(e) {
    let that = this;
    let showType = that.data.showType;
    showType = showType == 1 ? 2 : 1;

    that.setData({
      showType: showType
    })
  },

  /**
   * 分类弹框
   */
  category: function(e) {
    let that = this;
    let categoryStatus = that.data.categoryStatus;
    categoryStatus = categoryStatus == 0 ? 1 : 0;
    let maskStatus = categoryStatus == 1 ? 1 : 0;


    that.setData({
      categoryStatus: categoryStatus,
      sortStatus: 0,
      screenStatus: 0,
      maskStatus: maskStatus
    })
  },

  /**
   * 排序弹框
   */
  sort: function(e) {
    let that = this;
    let sortStatus = that.data.sortStatus;
    sortStatus = sortStatus == 0 ? 1 : 0;
    let maskStatus = sortStatus == 1 ? 1 : 0;

    that.setData({
      sortStatus: sortStatus,
      categoryStatus: 0,
      screenStatus: 0,
      maskStatus: maskStatus
    })
  },

  /**
   * 筛选弹框
   */
  screen: function(e) {
    let that = this;
    let screenStatus = that.data.screenStatus;
    screenStatus = screenStatus == 0 ? 1 : 0;
    let maskStatus = screenStatus == 1 ? 1 : 0;

    let goods_spec_array = that.data.goods_spec_array;
    if (app.checkArray(goods_spec_array)) {
      for (let index in goods_spec_array) {
        let values = goods_spec_array[index].values;
        for (let int in values) {
          if (values[int].is_selected == 1) {
            values[int].is_select = 1;
          } else {
            values[int].is_select = 0;
          }
        }
        goods_spec_array[index].values = values;
      }
    }

    let attr_or_spec = that.data.attr_or_spec;
    if (app.checkArray(attr_or_spec)) {
      for (let index in attr_or_spec) {
        let value_items = attr_or_spec[index].value_items;
        for (let int in value_items) {
          if (value_items[int].is_selected == 1) {
            value_items[int].is_select = 1;
          } else {
            value_items[int].is_select = 0;
          }
        }
        attr_or_spec[index].value_items = value_items;
      }
    }

    that.setData({
      screenStatus: screenStatus,
      categoryStatus: 0,
      sortStatus: 0,
      maskStatus: maskStatus,
      attr_or_spec: attr_or_spec,
      goods_spec_array: goods_spec_array
    })
  },

  /**
   * 关闭弹框
   */
  closePoupo: function(e) {
    let that = this;

    that.setData({
      maskStatus: 0,
      categoryStatus: 0,
      sortStatus: 0,
      screenStatus: 0,
      new_min_price: '',
      new_max_price: '',
    })
  },


  /**
   * 选择分类
   */
  selectCategory: function(e) {
    let that = this;
    let dataset = e.currentTarget.dataset;
    let category_id = dataset.id;
    let category_index = dataset.index;
    let goodsCategoryList = that.data.goodsCategoryList;

    if (category_index > -1 && goodsCategoryList[category_index].category_id == category_id && goodsCategoryList[category_index].child_list.length > 0) {
      that.setData({
        category_id: category_id,
        category_index: category_index,
      })
      return false;
    }
    category_id = category_id == 0 ? ' ' : category_id;
    app.sendRequest({
      url: 'System.Goods.goodsListByConditions',
      data: {
        page_index: 1,
        category_id: category_id
      },
      success: function(res) {
        let code = res.code;
        let data = res.data;
        if (code == 0) {
          let goods_list = data.goods_list.data;
          if (goods_list != undefined) {
            let screen = 0;
            if (goods_list[0] != undefined && category_id != 0) {
              screen = 1;
            }

            for (let index in goods_list) {
              let img = goods_list[index].pic_cover_small;
              goods_list[index].pic_cover_small = app.IMG(img);
            }
            // 属性数组处理
            let attr_or_spec = data.attr_or_spec;
            if (app.checkArray(attr_or_spec)) {
              for (let index in attr_or_spec) {
                delete attr_or_spec[index].is_search;
                delete attr_or_spec[index].type;
                delete attr_or_spec[index].sort;
                attr_or_spec[index]['open_height']=0;

                if (app.checkArray(attr_or_spec[index].value_items)) {
                  attr_or_spec[index].value_items.unshift('全部')
                  let spec_value = attr_or_spec[index].value_items;
                  for (let key in spec_value) {
                    let item_value = spec_value[key];
                    spec_value[key] = {};
                    spec_value[key]['value'] = item_value;
                    if (key == 0) {
                      spec_value[key]['is_select'] = 1;
                      spec_value[key]['is_selected'] = 1;
                    } else {
                      spec_value[key]['is_select'] = 0;
                      spec_value[key]['is_selected'] = 0;
                    }
                  }
                  attr_or_spec[index].value_items = spec_value;
                }
              }
            }

            // 规格数组处理
            let goods_spec_array = data.goods_spec_array;
            if (app.checkArray(goods_spec_array)) {
              for (let index in goods_spec_array) {
                delete goods_spec_array[index].create_time;
                delete goods_spec_array[index].show_type;
                delete goods_spec_array[index].sort;
                goods_spec_array[index]['open_height'] = 0;

                if (app.checkArray(goods_spec_array[index].values)) {
                  let obj = {
                    'spec_value_name': '全部'
                  }
                  goods_spec_array[index].values.unshift(obj)
                  let spec_value = goods_spec_array[index].values;
                  for (let key in spec_value) {
                    let item_value = spec_value[key];
                    spec_value[key] = {};
                    spec_value[key]['values'] = item_value;
                    if (key == 0) {
                      spec_value[key]['is_select'] = 1;
                      spec_value[key]['is_selected'] = 1;
                    } else {
                      spec_value[key]['is_select'] = 0;
                      spec_value[key]['is_selected'] = 0;
                      let spec_value_name = spec_value[key].values.spec_value_name;

                      let str = spec_value_name.indexOf('&nbsp;');
                      if(str){
                        spec_value_name = spec_value_name.replace(RegExp("&nbsp;", "g"), " ");
                      }
                      spec_value[key].values.spec_value_name = spec_value_name;
                    }
                  }
                  goods_spec_array[index].values = spec_value;
                }
              }
            }

            that.setData({
              goods_list: goods_list,
              category_brands: data.category_brands,
              attr_or_spec: attr_or_spec,
              goods_spec_array: goods_spec_array,
              min_price: '',
              max_price: '',
              next_page: 2,
              category_id: category_id,
              category_index: category_index,
              screen: screen,
            })

            that.closePoupo();
          }

        }
      },
    })
  },

  /**
   * 选择排序
   */
  selectSort: function(e) {
    let that = this;
    let dataset = e.currentTarget.dataset;
    let obyzd = dataset.obyzd;
    let st = dataset.st;
    let category_id = that.data.category_id;
    let salesSort = that.data.salesSort;
    let newSort = that.data.newSort;
    let priceSort = that.data.priceSort;
    let brand_id = that.data.brand_id;
    let min_price = that.data.min_price;
    let max_price = that.data.max_price;

    if (obyzd == 'sales') {
      salesSort = salesSort == 'desc' ? 'asc' : 'desc';

    } else if (obyzd == 'is_new') {
      newSort = newSort == 'desc' ? 'asc' : 'desc';

    } else if (obyzd == 'price') {
      priceSort = priceSort == 'desc' ? 'asc' : 'desc';

    } else {
      salesSort = 'desc';
      newSort = 'asc';
      priceSort = 'asc';
    }

    app.sendRequest({
      url: 'System.Goods.goodsListByConditions',
      data: {
        category_id: category_id,
        order: obyzd,
        sort: st,
        brand_id: brand_id,
        mipe: min_price,
        mape: max_price,
      },
      success: function(res) {
        let code = res.code;
        let data = res.data;
        if (code == 0) {
          let goods_list = data.goods_list.data;

          if (goods_list != undefined) {

            for (let index in goods_list) {
              let img = goods_list[index].pic_cover_small;
              goods_list[index].pic_cover_small = app.IMG(img);
            }
            that.setData({
              obyzd: obyzd,
              goods_list: goods_list,
              next_page: 2,
              category_id: category_id,
              salesSort: salesSort,
              newSort: newSort,
              priceSort: priceSort,
            })

            that.closePoupo();
          }
        }
      },
    })
  },

  /**
   * 选择品牌
   */
  selectBrand: function(e) {
    let that = this;
    let dataset = e.currentTarget.dataset;
    let brand_id = dataset.id;
    let category_id = that.data.category_id;
    let obyzd = that.data.obyzd;
    let salesSort = that.data.salesSort;
    let newSort = that.data.newSort;
    let priceSort = that.data.priceSort;
    let st = '';
    let min_price = that.data.min_price;
    let max_price = that.data.max_price;

    if (obyzd == 'sales') {
      st = salesSort;

    } else if (obyzd == 'is_new') {
      st = newSort;

    } else if (obyzd == 'price') {
      st = priceSort;

    }

    app.sendRequest({
      url: 'System.Goods.goodsListByConditions',
      data: {
        category_id: category_id,
        order: obyzd,
        sort: st,
        brand_id: brand_id,
        min_price: min_price,
        max_price: max_price,
      },
      success: function(res) {
        let code = res.code;
        let data = res.data;
        if (code == 0) {
          let goods_list = data.goods_list.data;

          if (goods_list != undefined) {

            for (let index in goods_list) {
              let img = goods_list[index].pic_cover_small;
              goods_list[index].pic_cover_small = app.IMG(img);
            }
            that.setData({
              obyzd: obyzd,
              goods_list: goods_list,
              next_page: 2,
              category_id: category_id,
              salesSort: salesSort,
              newSort: newSort,
              priceSort: priceSort,
            })

            that.closePoupo();
          }
        }
      },
    })
  },


  /**
   * 选择属性
   */
  selectAttr: function(e) {
    let that = this;
    let id = e.currentTarget.dataset.key;
    let index = e.currentTarget.dataset.index;
    let attr_item = that.data.attr_or_spec[index];
    let items = attr_item.value_items;

    let k = {};
    let parm = '';
    for (let index in items) {
      if (id == index) {
        items[index].is_select = 1;
      } else {
        items[index].is_select = 0;
      }
    }
    parm = 'attr_or_spec[' + index + '].value_items';
    k[parm] = items;
    that.setData(k)
  },

  

  /**
   * 属性项高度改变
   */
  attrHeight: function (e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let attr_or_spec = that.data.attr_or_spec;
    if (app.checkArray(attr_or_spec)){
      for (let int in attr_or_spec){
        if(int == index){
          if(attr_or_spec[int].open_height == 0){
            attr_or_spec[int].open_height = 1;
          }else{
            attr_or_spec[int].open_height = 0;
          }
        }
      }

      that.setData({
        attr_or_spec: attr_or_spec
      })
    }

  },

  /**
   * 规格项高度改变
   */
  specHeight: function (e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let goods_spec_array = that.data.goods_spec_array;
    if (app.checkArray(goods_spec_array)) {
      for (let int in goods_spec_array) {
        if (int == index) {
          if (goods_spec_array[int].open_height == 0) {
            goods_spec_array[int].open_height = 1;
          } else {
            goods_spec_array[int].open_height = 0;
          }
        }
      }

      that.setData({
        goods_spec_array: goods_spec_array
      })
    }

  },

  /**
   * 选择规格
   */
  selectSpec: function(e) {
    let that = this;
    let id = e.currentTarget.dataset.key;
    let index = e.currentTarget.dataset.index;
    let attr_item = that.data.goods_spec_array[index];
    let items = attr_item.values;

    let k = {};
    let parm = '';
    for (let index in items) {
      if (id == index) {
        items[index].is_select = 1;
      } else {
        items[index].is_select = 0;
      }
    }
    parm = 'goods_spec_array[' + index + '].values';
    k[parm] = items;
    that.setData(k)
  },

  /**
   * 价格
   */
  bindKeyInput: function(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    let min_price = that.data.new_min_price;
    let max_price = that.data.new_max_price;
    if (id == 'min_price') {
      min_price = e.detail.value;
    }
    if (id == 'max_price') {
      max_price = e.detail.value;
    }
    that.setData({
      new_min_price: min_price,
      new_max_price: max_price
    })
  },


  /**
   * 清除选项
   */
  clearOption: function (e) {
    let that = this;
    let attr_or_spec = that.data.attr_or_spec;
    if (app.checkArray(attr_or_spec)) {
      for (let index in attr_or_spec) {
        let value_items = attr_or_spec[index].value_items;
        for (let int in value_items) {
            if (int == 0) {
              value_items[int].is_select = 1;
            }else{
              value_items[int].is_select = 0;
            }
        }
        attr_or_spec[index].value_items = value_items;
      }
    }


    let goods_spec_array = that.data.goods_spec_array;
    if (app.checkArray(goods_spec_array)) {
      for (let index in goods_spec_array) {
        let values = goods_spec_array[index].values;
        for (let int in values) {
          if (int == 0) {
            values[int].is_select = 1;
          } else {
            values[int].is_select = 0;
          }
        }
        goods_spec_array[index].values = values;
      }
    }

    that.setData({
      goods_spec_array: goods_spec_array,
      attr_or_spec: attr_or_spec,
      min_price:null,
      max_price:null
    })

  },


  /**
   * 取消
   */
  cancel:function(){
    let that = this;
    let screenStatus = 0;
    let maskStatus = 0;
    that.setData({
      screenStatus: screenStatus,
      maskStatus: maskStatus
    })
  },


  /**
   * 确认价格区间
   */
  confirmPrice: function(e) {
    this.loadInfo();
    this.closePoupo();
  },


  loadInfo: function() {
    let that = this;
    let category_id = that.data.category_id;
    let obyzd = that.data.obyzd;
    let salesSort = that.data.salesSort;
    let newSort = that.data.newSort;
    let priceSort = that.data.priceSort;
    let st = '';
    let brand_id = that.data.brand_id;
    let min_price = that.data.new_min_price;
    let max_price = that.data.new_max_price;

    if (max_price == 0) {
      min_price = '';
      max_price = '';
    }

    if (obyzd == 'sales') {
      st = salesSort;

    } else if (obyzd == 'is_new') {
      st = newSort;

    } else if (obyzd == 'price') {
      st = priceSort;

    }

    let priceArr = [
      parseFloat(that.data.new_min_price),
      parseFloat(that.data.new_max_price)
    ];
    let keyword = that.data.search_text;

    let attr = '';
    let attr_or_spec = that.data.attr_or_spec;
    if (app.checkArray(attr_or_spec)) {
      for (let index in attr_or_spec) {
        let value_items = attr_or_spec[index].value_items;
        for (let int in value_items) {
          if (value_items[int].is_select == 1) {
            if(int != 0){
              attr += attr_or_spec[index].attr_value_name + ',' + value_items[int].value + ',' + attr_or_spec[index].attr_value_id + ';';
            }
            value_items[int].is_selected = 1;
          } else {
            value_items[int].is_selected = 0;
          }


        }
        attr_or_spec[index].value_items = value_items;
      }
    }


    let spec = '';
    let goods_spec_array = that.data.goods_spec_array;
    if (app.checkArray(goods_spec_array)) {
      for (let index in goods_spec_array) {
        let values = goods_spec_array[index].values;
        for (let int in values) {
          if (values[int].is_select == 1) {
            if(int != 0){
              spec += values[int].values.spec_id + ',' + values[int].values.spec_value_id + ';';
            }
            values[int].is_selected = 1;
          } else {
            values[int].is_selected = 0;
          }
         
        }
        console.log(values);
        goods_spec_array[index].values = values;  
      }
    }

    app.sendRequest({
      url: 'System.Goods.goodsListByConditions',
      data: {
        page_index: 1,
        brand_id: brand_id,
        category_id: category_id,
        order: obyzd,
        sort: st,
        attr: attr,
        spec: spec,
        brand_id: brand_id,
        min_price: min_price,
        max_price: max_price,
        keyword: keyword
      },
      success: function(res) {
        let code = res.code;
        let data = res.data;
        if (code == 0) {
          let goods_list = data.goods_list.data;
          let next_page = that.data.next_page++;
          let goodsCategoryList = data.goodsCategoryList;
          let category_index = that.data.category_index;

          for (let index in goodsCategoryList) {
            if (goodsCategoryList[index].category_id == category_id) {
              category_index = index;
            }
            let child_list = goodsCategoryList[index].child_list

            for (let key in child_list) {
              if (child_list[key].category_id == category_id) {
                category_index = index;
              }
            }
          }
          //图片处理
          for (let index in goods_list) {
            let img = goods_list[index].pic_cover_small;
            goods_list[index].pic_cover_small = app.IMG(img);
          }

          let screen = 0;
          if (goods_list != undefined) {
            if (goods_list[0] != undefined && category_id != 0 && category_id != '' && category_id != undefined) {
              screen = 1;
            }
          }

          that.setData({
            category_id: category_id,
            category_index: category_index,
            obyzd: obyzd,
            goods_list: goods_list,
            next_page: next_page,
            screen: screen,
            category_id: category_id,
            salesSort: salesSort,
            newSort: newSort,
            priceSort: priceSort,
            min_price: min_price,
            max_price: max_price,
            attr: attr,
            spec: spec,
            goods_spec_array: goods_spec_array,
            attr_or_spec: attr_or_spec
          })
        }

      }
    });
  }
})